/**
 * 
 */
package neptune.web.client.model.topology;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class SerializableNode implements IsSerializable {

	/*
	 * Descrizione testuale a scopo documentale
	 */
	private String description;
	/*
	 * Id univoco del nodo
	 */
	private String id;
	
	/**
	 * il nome del nodo
	 */
	private String name;
	/*
	 * Una stringa che indica il tipo di cpu
	 */
	private SerializableCpu cpu;
	/*
	 * la ram del nodo espressa in MB
	 */
	private int ram;
	/*
	 * lo spazio su disco espresso in GB
	 */
	private float hd;
	/*
	 * Le interfacce di rete possedute da questo nodo
	 */
	private ArrayList<SerializableNetworkInterface> interfaces = new ArrayList<SerializableNetworkInterface>(
			0);
	/*
	 * La sigla del sistema operativo installato sul nodo
	 */
	private SerializableOperativeSystem operativeSystem;
	/*
	 * L'identificativo del software template usato da questo nodo
	 */
	private String softwareTemplate;
	/*
	 * Le componenti software installate sul nodo
	 */
	private ArrayList<SerializableSoftware> installedSoftwares = new ArrayList<SerializableSoftware>(
			0);
	/*
	 * L'eventuale hypervisor in esecuzione sul nodo
	 */
	private SerializableHypervisor hypervisor;
	/*
	 * Gli eventuali nodi virtuali opsitati su questo nodo
	 */
	private ArrayList<String> hostedNodes = new ArrayList<String>(0);
	/*
	 * Specifica se questo nodo � fisico (false) o se e' virtuale e quindi
	 * ospitato da un altro nodo (true)
	 */
	private boolean isVirtual;
	/*
	 * Indica l'hoster per questo nodo, null se il nodo non e' ospitato da
	 * nessun nodo, perche' non allocato o perche' nodo fisico.
	 */
	private String hosterId;
	/*
	 * La porta su cui � in ascolto il server ssh per controllare questo nodo
	 */
	private int controlPort = 22;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		SerializableNode node = (SerializableNode) obj;
		if (node.getId().equals(this.id))
			return true;
		return false;
	}

	/**
	 * @return the controlPort
	 */
	public int getControlPort() {
		return controlPort;
	}

	/**
	 * @return the cpu
	 */
	public SerializableCpu getCpu() {
		return cpu;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the hd
	 */
	public float getHd() {
		return hd;
	}

	/**
	 * @return the hostedNodes
	 */
	public ArrayList<String> getHostedNodes() {
		return hostedNodes;
	}

	/**
	 * @return the hosterId
	 */
	public String getHosterId() {
		return hosterId;
	}

	/**
	 * @return the hypervisor
	 */
	public SerializableHypervisor getHypervisor() {
		return hypervisor;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return the installedSoftwares
	 */
	public ArrayList<SerializableSoftware> getInstalledSoftwares() {
		return installedSoftwares;
	}

	/**
	 * 
	 * @param interfaceId
	 * @return
	 */
	public SerializableNetworkInterface getInterface(String interfaceId) {
		for (SerializableNetworkInterface netIf : interfaces) {
			if (netIf.getInterfaceName().equals(interfaceId))
				return netIf;
		}
		return null;
	}

	public SerializableNetworkInterface getInterfaceByName(String ifName) {
		for (SerializableNetworkInterface netIf : interfaces) {
			if (netIf.getInterfaceName().equals(ifName))
				return netIf;
		}

		return null;
	}

	/**
	 * @return the interfaces
	 */
	public ArrayList<SerializableNetworkInterface> getInterfaces() {
		return interfaces;
	}

	public String getName()
	{
		return this.name;
	}

	/**
	 * @return the operativeSystem
	 */
	public SerializableOperativeSystem getOperativeSystem() {
		return operativeSystem;
	}

	/**
	 * @return the ram
	 */
	public int getRam() {
		return ram;
	}

	/**
	 * @return the softwareTemplate
	 */
	public String getSoftwareTemplate() {
		return softwareTemplate;
	}

	/**
	 * @return the isVirtual
	 */
	public boolean isVirtual() {
		return isVirtual;
	}

	public void removeNetInterface(SerializableNetworkInterface interf) {
		for (int i = 0; i < interfaces.size(); i++) {
			if (interfaces.get(i).getInterfaceName().equals(
					interf.getInterfaceName())) {
				interfaces.get(i).setParentNode(null);
				interfaces.remove(i);
				break;
			}
		}
	}

	public void removeSoftware(SerializableSoftware soft) {
		for (int i = 0; i < installedSoftwares.size(); i++) {
			if (installedSoftwares.get(i).getId().equals(soft.getId())) {
				installedSoftwares.remove(i);
				break;
			}
		}
	}

	/**
	 * @param controlPort
	 *            the controlPort to set
	 */
	public void setControlPort(int controlPort) {
		this.controlPort = controlPort;
	}

	/**
	 * @param cpu
	 *            the cpu to set
	 */
	public void setCpu(SerializableCpu cpu) {
		this.cpu = cpu;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param hd
	 *            the hd to set
	 */
	public void setHd(float hd) {
		this.hd = hd;
	}

	/**
	 * @param hostedNodes
	 *            the hostedNodes to set
	 */
	public void setHostedNodes(ArrayList<String> hostedNodes) {
		this.hostedNodes = hostedNodes;
	}

	/**
	 * @param hosterId
	 *            the hosterId to set
	 */
	public void setHosterId(String hosterId) {
		this.hosterId = hosterId;
	}
	
	/**
	 * @param hypervisor
	 *            the hypervisor to set
	 */
	public void setHypervisor(SerializableHypervisor hypervisor) {
		this.hypervisor = hypervisor;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	

	/**
	 * @param installedSoftwares
	 *            the installedSoftwares to set
	 */
	public void setInstalledSoftwares(
			ArrayList<SerializableSoftware> installedSoftwares) {
		this.installedSoftwares = installedSoftwares;
	}

	/**
	 * @param interfaces
	 *            the interfaces to set
	 */
	public void setInterfaces(ArrayList<SerializableNetworkInterface> interfaces) {
		this.interfaces = interfaces;
		for (SerializableNetworkInterface serializableNetworkInterface : interfaces) {
			serializableNetworkInterface.setParentNode(id);
		}
	}

	public void setName(String name)
	{
		this.name=name;
	}

	/**
	 * @param operativeSystem
	 *            the operativeSystem to set
	 */
	public void setOperativeSystem(SerializableOperativeSystem operativeSystem) {
		this.operativeSystem = operativeSystem;
	}

	/**
	 * @param ram
	 *            the ram to set
	 */
	public void setRam(int ram) {
		this.ram = ram;
	}

	/**
	 * @param softwareTemplate
	 *            the softwareTemplate to set
	 */
	public void setSoftwareTemplate(String softwareTemplate) {
		this.softwareTemplate = softwareTemplate;
	}

	/**
	 * @param isVirtual
	 *            the isVirtual to set
	 */
	public void setVirtual(boolean isVirtual) {
		this.isVirtual = isVirtual;
	}
}
